package org.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
public LoginFilter(){
   super(); }

public void destroy() {}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
   HttpSession session=((HttpServletRequest)request).getSession();
   response.setCharacterEncoding("UTF-8");
   request.setCharacterEncoding("UTF-8");
 if(session.getAttribute("me")==null){
    PrintWriter out=response.getWriter();
    out.print("<script>alert('请登录！');location.href='/MyChat/servlet/UserServlet'</script>");
  }
  else{
// pass the request along the filter chain
     chain.doFilter(request, response);
     }
}

public void init(FilterConfig fConfig) throws ServletException {
   // TODO Auto-generated method stub
  this.filterConfig=fConfig;
}

}